package com.ytwl.service;

import com.ytwl.mapper.CarriersMapper;
import com.ytwl.mapper.SchedulingMapper;
import com.ytwl.bean.Carriers;
import com.ytwl.bean.Scheduling;
import com.ytwl.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ScheService {
    @Autowired
    CarriersMapper carriersMapper;
    @Autowired
    SchedulingMapper schedulingMapper;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    public Long getCarriersTotal(Integer state) {
        return carriersMapper.getTotal2(state);
    }

    public List<Carriers> getAllCarriers(Integer page, Integer size, Integer state) {
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        return carriersMapper.getAllCarriers(page, size, state);
    }

    public boolean updateSche(Integer carriersid, Integer truckid, String startTime) {
        try {
            Carriers carriers = new Carriers();
            carriers.setFinishedstate(1);
            carriers.setCarriersid(carriersid);
            carriersMapper.updateByPrimaryKeySelective(carriers);
            Scheduling scheduling = new Scheduling();
            scheduling.setFkCarriersid(carriersid);
            scheduling.setFkTruckid(truckid);
            scheduling.setStarttime(sdf.parse(startTime));
            scheduling.setFkUserid(UserUtils.getCurrentUser().getUserid());
            scheduling.setCheckintime(new Date());
            return schedulingMapper.insertSelective(scheduling) == 1;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

    public List<Scheduling> getAllScheduling(Integer page, Integer size) {
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        return schedulingMapper.getAllScheduling(page,size);
    }

    public Long getTotal() {
        return schedulingMapper.getTotal();
    }
}
